package jk.fox.exchange.server.handler;

import jk.fox.common.RpcStatus;
import jk.fox.common.RpcURIUtils;
import jk.fox.common.payload.request.Request;
import jk.fox.common.payload.response.RpcResponse;
import jk.fox.invocation.Invocation;
import org.apache.log4j.Logger;

/**
 * @author cuichao
 * @Description: StandardExchangeHandler
 * @Date: create in 2020/4/19 14:29
 */
public class StandardExchangeHandler implements ExchangeHandler {

    private static Logger logger = Logger.getLogger(StandardExchangeHandler.class);

    private Invocation invocation;

    public StandardExchangeHandler(Invocation invocation) {
        this.invocation = invocation;
    }

    @Override
    public RpcResponse handle(Request request) throws Exception {
        String[] domainAndPath = RpcURIUtils.getDomainAndPath(request.getPath());
        String domain = domainAndPath[0];
        String path = domainAndPath[1];
        logger.debug("ServerExchange handle the request this requestId is "+ request.getRequestId());
        //执行前环绕可以埋点
        Object invoke = invocation.remote(domain,path, request.getParameterTypes(),request.getParameters());
        //执行后环绕可以埋点
        RpcResponse response = new RpcResponse();
        response.setRequestId(request.getRequestId());
        response.setResponseCode(RpcStatus.OK);
        response.setResponse(invoke);
        logger.debug("ServerExchange handle the success response this requestId is "+ request.getRequestId());
        return response;
    }
}
